﻿using gregn6Lib;
using NursingFormSystem.Common;
using NursingFormSystem.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace NursingFormSystem.NeonatalObservationTable
{
    public partial class PrintNeonatalObservationTable : Form
    {
        #region 变量声明
        private GridppReport Report = new GridppReport();
        NurFormsService.NursingForm formService = new NurFormsService.NursingForm();
        PatientInfo PatientInfo;
        Tb_User LoginUser;
        DateTime StatDate;
        DateTime EndDate;
        #endregion

        #region 构造函数
        public PrintNeonatalObservationTable(Tb_User loginUser, PatientInfo patientInfo, DateTime StatDate, DateTime EndDate)
        {
            InitializeComponent();
            PatientInfo = patientInfo;
            LoginUser = loginUser;
            this.StatDate = StatDate;
            this.EndDate = EndDate;
            //载入报表模板数据
            Report.LoadFromFile(System.Windows.Forms.Application.StartupPath + @"\GRFModel\Ydhl_GRF\新生儿观察表.grf");
            //设置与数据源的连接串，因为在设计时指定的数据库路径是绝对路径。
            Report.ConnectionString = Utility.GetDatabaseConnectionString();
            //连接报表取数事件
            Report.FetchRecord += new _IGridppReportEvents_FetchRecordEventHandler(ReportFetchRecord);
            //设定查询显示器关联的报表
            axGRPrintViewer1.Report = Report;
        }
        #endregion

        #region 获取链接报表事件
        public void ReportFetchRecord()
        {
            var ret = formService.GetNursingFormRecordByDate(this.PatientInfo.PATIENT_ID, this.PatientInfo.VISIT_ID, (int)FormType.BabyBornObserve, StatDate, EndDate);
            DataTable dt = new DataTable();
            if (ret.Code == 1)
            {
                CreateDataTableHead(dt);
                List<NursingFormRecord> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<NursingFormRecord>>(ret.D);
                list = list.OrderBy(p => p.AssessmentDate).ToList();
                for (int i = 0; i < list.Count; i++)
                {
                    NursingFormRecord record = list[i];
                    NeonatalObservation noo = XmlUtil.XMLToObject<NeonatalObservation>(record.NursingFormData, Encoding.UTF8);
                    DataRow newRow = dt.NewRow();
                    newRow["PATIENT_NAME"] = PatientInfo.PATIENT_NAME;  //患者姓名
                    newRow["SEX"] = PatientInfo.SEX;                    //性别
                    newRow["AGE"] = PatientInfo.AGE;                    //年龄
                    newRow["BED_NO"] = PatientInfo.BED_NO;              //床号
                    newRow["PATIENT_ID"] = PatientInfo.PATIENT_ID;      //住院号
                    newRow["RiQi"] = noo.RiQi;                          //日期
                    newRow["ShiJian"] = noo.ShiJian;                    //时间
                    newRow["ChenJianHuLi"] = noo.ChenJianHuLi;          //晨间护理
                    newRow["ChiNaiLiang"] = noo.ChiNaiLiang;            //吃奶量
                    newRow["JiaNai"] = noo.JiaNai;                      //加奶
                    newRow["YuNai"] = noo.YuNai;                        //余奶
                    newRow["MuRu"] = noo.MuRu;                          //母乳
                    newRow["NiaoLiang"] = noo.NiaoLiang;                //尿量
                    newRow["NiaoSe"] = noo.NiaoSe;                      //尿色
                    newRow["FenLiang"] = noo.FenLiang;                  //粪量
                    newRow["FenSe"] = noo.FenSe;                        //粪色
                    newRow["FenXingZhuang"] = noo.FenXingZhuang;        //粪性状
                    newRow["WenXiang"] = noo.WenXiang;                  //温箱
                    newRow["ShiDu"] = noo.ShiDu;                        //湿度
                    newRow["LanGuangLiao"] = noo.LanGuangLiao;          //蓝光疗
                    newRow["HR"] = noo.HR;                              //HR
                    newRow["R"] = noo.R;                                //R
                    newRow["SPO"] = noo.SPO;                            //SPO
                    newRow["BP"] = noo.BP;                              //Bp
                    newRow["TuNaiLiang"] = noo.TuNaiLiang;              //吐奶量
                    newRow["QianMing"] = noo.QianMing;                  //签名
                    dt.Rows.Add(newRow);
                }
                dt.DefaultView.Sort = " RiQi ASC, ShiJian ASC";
                DataTable data = dt.DefaultView.ToTable();
                //将 DataTable 的数据转储到 Grid++Report 的数据集中
                Utility.FillRecordToReport(Report, data);
            }
            else if (ret.Code == 2)
            {
                CreateDataTableHead(dt);
                DataRow dr = dt.NewRow();
                dt.Rows.Add(dr);
                Utility.FillRecordToReport(Report, dt);
            }
            else if (ret.Code == 0)
            {
                MessageBox.Show(ret.M);
            }
        }

        #endregion

        #region 显示列头
        private void CreateDataTableHead(DataTable dt)
        {
            dt.Columns.Add("PATIENT_NAME", typeof(string));    //患者姓名
            dt.Columns.Add("SEX", typeof(string));             //性别
            dt.Columns.Add("AGE", typeof(string));             //年龄
            dt.Columns.Add("BED_NO", typeof(string));          //床号
            dt.Columns.Add("PATIENT_ID", typeof(string));      //住院号
            dt.Columns.Add("RiQi", typeof(string));            //日期
            dt.Columns.Add("ShiJian", typeof(string));         //时间
            dt.Columns.Add("ChenJianHuLi", typeof(string));    //晨间护理
            dt.Columns.Add("ChiNaiLiang", typeof(string));     //吃奶量
            dt.Columns.Add("JiaNai", typeof(string));          //加奶
            dt.Columns.Add("YuNai", typeof(string));           //余奶
            dt.Columns.Add("MuRu", typeof(string));            //母乳
            dt.Columns.Add("NiaoLiang", typeof(string));       //尿量
            dt.Columns.Add("NiaoSe", typeof(string));          //尿色
            dt.Columns.Add("FenLiang", typeof(string));        //粪量
            dt.Columns.Add("FenSe", typeof(string));           //粪色
            dt.Columns.Add("FenXingZhuang", typeof(string));   //粪性状
            dt.Columns.Add("WenXiang", typeof(string));        //温箱
            dt.Columns.Add("ShiDu", typeof(string));           //湿度
            dt.Columns.Add("LanGuangLiao", typeof(string));    //蓝光疗
            dt.Columns.Add("HR", typeof(string));              //HR
            dt.Columns.Add("R", typeof(string));               //R
            dt.Columns.Add("SPO", typeof(string));             //SPO
            dt.Columns.Add("BP", typeof(string));              //Bp
            dt.Columns.Add("TuNaiLiang", typeof(string));      //吐奶量
            dt.Columns.Add("QianMing", typeof(string));        //签名
        }      
        #endregion

        #region 窗体加载启动锐浪报表
        private void PrintNeonatalObservationTable_Load(object sender, EventArgs e)
        {
            axGRPrintViewer1.Start();
        }
        #endregion                                         
    }
}